package controller

import (
	"fabric-explorer/tools"

	"github.com/gin-gonic/gin"
)

const (
	CHAINCODE_BASIC          = "basic"
	CHAINCODE_ASSET_TRANSFER = "asset_transfer"
)

//basic链码 CreateAsset
func BasicCreateAsset(c *gin.Context) {
	id := c.Query("id")
	color := c.Query("color")
	size := c.Query("size")
	owner := c.Query("owner")
	appraisedValue := c.Query("appraisedValue")
	if id == "" || color == "" || size == "" || owner == "" || appraisedValue == "" {
		tools.FailResult(c, "chaincode<Basic> CreateAsset paras invalid")
		return
	}
	result, err := tools.ExecuteChaincode(CHAINCODE_BASIC, "CreateAsset", id, color, size, owner, appraisedValue)
	if err != nil {
		tools.FailResult(c, err.Error())
		return
	}
	tools.SuccessResult(c, result)
}

//basic链码 ReadAsset
func BasicReadAsset(c *gin.Context) {
	id := c.Query("id")
	if id == "" {
		tools.FailResult(c, "chaincode<Basic> ReadAsset paras invalid")
		return
	}
	result, err := tools.QueryChaincode(CHAINCODE_BASIC, "ReadAsset", id)
	if err != nil {
		tools.FailResult(c, err.Error())
		return
	}
	tools.SuccessResult(c, result)
}

//basic链码 TransferAsset
func BasicTransferAsset(c *gin.Context) {
	id := c.Query("id")
	newOwner := c.Query("newOwner")
	if id == "" || newOwner == "" {
		tools.FailResult(c, "chaincode<Basic> TransferAsset paras invalid")
		return
	}
	result, err := tools.ExecuteChaincode(CHAINCODE_BASIC, "TransferAsset", id, newOwner)
	if err != nil {
		tools.FailResult(c, err.Error())
		return
	}
	tools.SuccessResult(c, result)
}

//asset_transfer链码 RegisterUser
//userId,userName
func AssetRegisterUser(c *gin.Context) {
	userId := c.Query("userId")
	userName := c.Query("userName")
	if userId == "" || userName == "" {
		tools.FailResult(c, "chaincode<"+CHAINCODE_ASSET_TRANSFER+"> RegisterUser paras invalid")
		return
	}
	result, err := tools.ExecuteChaincode(CHAINCODE_ASSET_TRANSFER, "RegisterUser", userId, userName)
	if err != nil {
		tools.FailResult(c, err.Error())
		return
	}
	tools.SuccessResult(c, result)
}

//asset_transfer链码 ObtainBalance
//userId,userName
func AssetObtainBalance(c *gin.Context) {
	userId := c.Query("userId")
	balance := c.Query("balance")
	if userId == "" || balance == "" {
		tools.FailResult(c, "chaincode<"+CHAINCODE_ASSET_TRANSFER+"> ObtainBalance paras invalid")
		return
	}
	result, err := tools.ExecuteChaincode(CHAINCODE_ASSET_TRANSFER, "ObtainBalance", userId, balance)
	if err != nil {
		tools.FailResult(c, err.Error())
		return
	}
	tools.SuccessResult(c, result)
}

//asset_transfer链码 BalanceOf
//userId
func AssetBalanceOf(c *gin.Context) {
	userId := c.Query("userId")
	if userId == "" {
		tools.FailResult(c, "chaincode<"+CHAINCODE_ASSET_TRANSFER+"> BalanceOf paras invalid")
		return
	}
	result, err := tools.QueryChaincode(CHAINCODE_ASSET_TRANSFER, "BalanceOf", userId)
	if err != nil {
		tools.FailResult(c, err.Error())
		return
	}
	tools.SuccessResult(c, result)
}

//asset_transfer链码 QueryBalanceHistory
//userId
func AssetQueryBalanceHistory(c *gin.Context) {
	userId := c.Query("userId")
	if userId == "" {
		tools.FailResult(c, "chaincode<"+CHAINCODE_ASSET_TRANSFER+"> QueryBalanceHistory paras invalid")
		return
	}
	result, err := tools.QueryChaincode(CHAINCODE_ASSET_TRANSFER, "QueryBalanceHistory", userId)
	if err != nil {
		tools.FailResult(c, err.Error())
		return
	}
	tools.SuccessResult(c, result)
}

//asset_transfer链码 AssetEnroll
//assetId string, assetName string, assetOwner string, assetMetaData string, asssetValue int
func AssetEnroll(c *gin.Context) {
	assetId := c.Query("assetId")
	assetName := c.Query("assetName")
	assetOwner := c.Query("assetOwner")
	assetMetaData := c.Query("assetMetaData")
	asssetValue := c.Query("asssetValue")
	if assetId == "" || assetName == "" || assetOwner == "" || asssetValue == "" {
		tools.FailResult(c, "chaincode<"+CHAINCODE_ASSET_TRANSFER+"> AssetEnroll paras invalid")
		return
	}
	result, err := tools.ExecuteChaincode(CHAINCODE_ASSET_TRANSFER, "AssetEnroll", assetId, assetName, assetOwner, assetMetaData, asssetValue)
	if err != nil {
		tools.FailResult(c, err.Error())
		return
	}
	tools.SuccessResult(c, result)
}

//asset_transfer链码 AssetTransfer
//assetId string, fromUserId string, toUserId string
func AssetTransfer(c *gin.Context) {
	assetId := c.Query("assetId")
	fromUserId := c.Query("fromUserId")
	toUserId := c.Query("toUserId")
	if assetId == "" || fromUserId == "" || toUserId == "" {
		tools.FailResult(c, "chaincode<"+CHAINCODE_ASSET_TRANSFER+"> AssetTransfer paras invalid")
		return
	}
	result, err := tools.ExecuteChaincode(CHAINCODE_ASSET_TRANSFER, "AssetTransfer", assetId, fromUserId, toUserId)
	if err != nil {
		tools.FailResult(c, err.Error())
		return
	}
	tools.SuccessResult(c, result)
}

//asset_transfer链码 QueryAsset
//assetId string, fromUserId string, toUserId string
func AssetQuery(c *gin.Context) {
	assetId := c.Query("assetId")
	if assetId == "" {
		tools.FailResult(c, "chaincode<"+CHAINCODE_ASSET_TRANSFER+"> QueryAsset paras invalid")
		return
	}
	result, err := tools.QueryChaincode(CHAINCODE_ASSET_TRANSFER, "QueryAsset", assetId)
	if err != nil {
		tools.FailResult(c, err.Error())
		return
	}
	tools.SuccessResult(c, result)
}

//asset_transfer链码 QueryAssetTransaction
//assetId string
func AssetQueryTransaction(c *gin.Context) {
	assetId := c.Query("assetId")
	if assetId == "" {
		tools.FailResult(c, "chaincode<"+CHAINCODE_ASSET_TRANSFER+"> QueryAssetTransaction paras invalid")
		return
	}
	result, err := tools.QueryChaincode(CHAINCODE_ASSET_TRANSFER, "QueryAssetTransaction", assetId)
	if err != nil {
		tools.FailResult(c, err.Error())
		return
	}
	tools.SuccessResult(c, result)
}

//asset_transfer链码 QueryUserAssets
//userId string
func AssetQueryUserAssets(c *gin.Context) {
	userId := c.Query("userId")
	if userId == "" {
		tools.FailResult(c, "chaincode<"+CHAINCODE_ASSET_TRANSFER+"> QueryUserAssets paras invalid")
		return
	}
	result, err := tools.QueryChaincode(CHAINCODE_ASSET_TRANSFER, "QueryUserAssets", userId)
	if err != nil {
		tools.FailResult(c, err.Error())
		return
	}
	tools.SuccessResult(c, result)
}
